
$(function(){
    ajaxUserHtml("");
});

var ajaxUserHtml = function(deptId){
    $.ajax({
        url : "/sys/userGroup/listUserPage?deptId="+deptId,
        type : "get",
        dataType : "html",
        success : function(r) {
            $("#userChild").html(r);
        }
    });
};

var setting = {
    showLine : true,
    view: {
        selectedMulti: false,
        showTitle : true
    },
    async: {
        enable: true,
        url:"/sys/dept/zTree",
        autoParam:["id"],
        type: "post",
        dataFilter: filter
    },
    data: {
        simpleData: {
            enable : true,
            idKey : "id",		// 结点的id,对应到Json中的id
            pIdKey : "pId"      // 结点的pId,对应到Json中的pId
        },
        key: {
            title: "title"
        }
    },
    callback : {
        onClick : showDetail			// 	点击节点时，显示节点详细信息
        ,
        onAsyncSuccess: zTreeOnAsyncSuccess  // 异步加载正
    }

};



//用于捕获异步加载正常结束的事件回调函数
function zTreeOnAsyncSuccess(event, treeId, treeNode, msg){
    var treeObj = $.fn.zTree.getZTreeObj(treeId);
    var nodes = treeObj.getNodes();
    if (nodes.length>0) {
        for(var i=0;i<nodes.length;i++){
            treeObj.expandNode(nodes[i], true, false, false);//默认展开第一级节点
        }
    }
}

function filter(treeId, parentNode, childNodes) {
    if (!childNodes) return null;
    for (var i=0, l=childNodes.length; i<l; i++) {
        childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
    }
    return childNodes;
}

$(document).ready(function(){
    $.fn.zTree.init($("#departtree"), setting);
    /*$.ajax({
        async : false,
        cache:false,
        type: 'POST',
        dataType : "json",
        url: "/sys/dept/zTreeRoot",
        error: function () {//请求失败处理函数
            alert('请求失败');
        },
        success:function(data){ //请求成功后处理函数。
            //把后台封装好的简单Json格式赋给treeNodes
            $.fn.zTree.init($("#departtree"), setting,eval(data));
        }
    });*/
});

function showDetail(event, treeId, treeNode, clickFlag){
    var detpId = treeNode.id;
    ajaxUserHtml(detpId);
}